const Web3 = require("web3");
const shelljs = require("shelljs");
const fs = require('fs');
(async () => {
    // 合约abi

// 合约实例
    let instance;
// 链上账户信息
    let accounts;
// 合约地址,可以从链上查看或者从 Ganche 客户端查看
    const loansAddress = '0x428132a9D50a62b3fF9B1D4Ad8852E1EaA468f95'
    const source = fs.readFileSync('/Users/binny/blockchainProjects/trufflePro/solidity-note/build/contracts/BNB.json')
//创建一个 web3 示例
    const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:7545'))
    accounts = await web3.eth.getAccounts();
    console.log(accounts);
    // let abi_path = './instance/abi.txt';//  node ./test/event.js
    let {
        loansAbi,
    } = JSON.parse(source);
    instance = new web3.eth.Contract(loansAbi, loansAddress, {
        from: accounts[0]
    });
    // console.log(instance);

    /**
     * 此方式只输出一次事件
     */
    instance.getPastEvents('Msg',{fromBlock:168},function (error, result) {
        if (error) {
            console.error(error);
        } else {
            console.log(result)
        }
    });

    /**
     * 持续监听事件
     */
    instance.events.set_age( {fromBlock: 168}, function (error, result) {
        if (error) {
            console.error(error);
        } else {
            console.log(result)
        }
    })
})()
